﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NewGlobalBizAssist.UControl
{




    public partial class UC_SearchMember : System.Web.UI.UserControl
    {

        public delegate void FindUpdateEventHandler(object sender,Lib.FindUpdateEventArgs e);

        // add an event of the delegate type
        public event FindUpdateEventHandler FindHandler;



        string _pagename = "";

        public string PageName
        {
            set { _pagename = value; }
        }

        public void HideSearch()
        {
            gvMembers.Visible = false;
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Enable the GridView paging option and  
                // specify the page size. 
                gvMembers.AllowPaging = true;
                gvMembers.PageSize = 15;
               

                // Enable the GridView sorting option. 
                gvMembers.AllowSorting = true;


                // Initialize the sorting expression. 
                ViewState["SortExpression"] = "ElementID";
                

                // Populate the GridView. 
                //BindGridView();
            } 
        }

        private void BindGridView(string txt)
        {
            gvMembers.Visible = true;
            gvMembers.DataSource = new Lib.Member().GetMember(txt);
            gvMembers.DataBind();

        }

        // GridView.PageIndexChanging Event 
        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // Set the index of the new display page.  
            gvMembers.PageIndex = e.NewPageIndex;


            // Rebind the GridView control to  
            // show data in the new page. 
            BindGridView(txtsrch.Text);
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Make sure the current GridViewRow is a data row. 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Make sure the current GridViewRow is either  
                // in the normal state or an alternate row. 
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    // Add client-side confirmation when deleting. 

                   // ((HyperLink)e.Row.Cells[0].Controls[1]).NavigateUrl =   this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx?ID=" + ((HyperLink)e.Row.Cells[0].Controls[1]).Text;
                }
            }
        }


        // GridView.Sorting Event 
        protected void gv_Sorting(object sender, GridViewSortEventArgs e)
        {
            string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');


            // If the sorting column is the same as the previous one,  
            // then change the sort order. 
            if (strSortExpression[0] == e.SortExpression)
            {
                if (strSortExpression[1] == "ASC")
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
                }
            }
            // If sorting column is another column,   
            // then specify the sort order to "Ascending". 
            else
            {
                ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
            }


            // Rebind the GridView control to show sorted data. 
            BindGridView(txtsrch.Text);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView(txtsrch.Text);
        }

        protected void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
 
            if (e.CommandName == "Select")
            {
 
                int index = Convert.ToInt32(e.CommandArgument);


                GridViewRow row = gvMembers.Rows[index];
                 
                // raise the event with the updated arguments
                FindHandler(this, new Lib.FindUpdateEventArgs(Server.HtmlDecode(row.Cells[1].Text)));
                gvMembers.Visible = false;
 

            }
        }   

    }
}